package junit.admin;

import com.jfinal.aop.Inject;
import io.jboot.test.MockMvcResult;
import io.jpress.model.Role;
import io.jpress.service.RoleService;
import io.jpress.web.functions.PermissionFunctions;
import junit.base.AdminBaseTester;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class AdminRoleTester extends AdminBaseTester {


    @Inject
    private RoleService roleService;


    @Test
    public void startRoleTestCases() {

        //用户登录，默认账号 admin 123456
        doLogin();

        //查看角色的权限列表
        testQueryRolePermissions();

        //新增角色
        testAddRole();


        //更新角色信息
        testUpdateRole();


        //为角色分配权限
        testSetPermissionsToRole();


        //测试该角色是否有分配权限
        testSetPermissionsToRole();


        //删除角色
        testDeleteRole();

    }


    /**
     * 新增角色
     */
    public void testQueryRolePermissions() {

        MockMvcResult mvcResult = mvc.get("/admin/user/role/permissions/1");

        Assert.assertTrue(mvcResult.getContent().contains("菜单权限"));
        Assert.assertTrue(mvcResult.getContent().contains("功能权限"));
        Assert.assertTrue(mvcResult.getContent().contains("逻辑权限"));
        Assert.assertTrue(mvcResult.getContent().contains("敏感数据权限"));

//        mvcResult.printResult();
    }


    /**
     * 新增角色
     */
    public void testAddRole() {
        Role role = new Role();

        Map<String, Object> paras = buildParas(role, "role");
        MockMvcResult mvcResult = postWithCsrfToken("/admin/user/role/doSave", paras);
        Assert.assertTrue(mvcResult.getContent().contains("角色名称不能为空"));


        role.setName("test title");
        paras = buildParas(role, "role");
        mvcResult = postWithCsrfToken("/admin/user/role/doSave", paras);
        Assert.assertTrue(mvcResult.getStatus() == 200);
        Assert.assertTrue(mvcResult.getContentAsJSONObject().get("state").equals("ok"));

        Long roleId = mvcResult.getContentAsJSONObject().getLong("id");
        putContainerData("roleId", roleId);

        mvcResult.printResult();
    }


    /**
     * 修改角色信息
     */
    public void testUpdateRole() {

        Role role = new Role();
        role.setName("new Name");
        role.setId((Long) getContainerData("roleId"));

        Map<String, Object> paras = buildParas(role, "role");

        MockMvcResult mvcResult = postWithCsrfToken("/admin/user/role/doSave", paras);
        mvcResult.printResult();

        Assert.assertTrue(mvcResult.getStatus() == 200);
        Assert.assertTrue(mvcResult.getContentAsJSONObject().get("state").equals("ok"));

        Long roleId = mvcResult.getContentAsJSONObject().getLong("id");
        putContainerData("roleId", roleId);

        final Role byId = roleService.findById(roleId);
        Assert.assertEquals(byId.getName(), role.getName());
    }


    public void testSetPermissionsToRole() {

        Map<String, Object> paras = new HashMap<>();
        paras.put("roleId", getContainerData("roleId"));

        //分配 menu 权限
        paras.put("permissionId", "menu:/admin/department/list");

        MockMvcResult mvcResult = getWithCsrfToken("/admin/user/doAddRolePermission", paras);
        mvcResult.printResult();

        Assert.assertEquals(mvcResult.getStatus(),200);
    }


    public void testRoleHasPermission() {

        Role role = roleService.findById(getContainerData("roleId"));
        final boolean hasPermission = PermissionFunctions.hasPermission(role, "menu:/admin/department/list");
        Assert.assertTrue(hasPermission);

    }


    /**
     * 删除角色
     */
    public void testDeleteRole() {
        Long roleId = (Long) getContainerData("roleId");
        MockMvcResult mvcResult = getWithCsrfToken("/admin/user/role/doDel/" + roleId);
        mvcResult.printResult();

        Assert.assertTrue(mvcResult.getStatus() == 200);
    }


}
